//
//  HMMainTabBarController.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


class HMMainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置自定义tabBar
        let mainTabBar = HMTabBar()
        /*
        mainTabBar.btn.addTarget(self, action: #selector(btnClick), forControlEvents: .TouchUpInside)
 */
        mainTabBar.btnClickClosure = {
            self.btnClick()
        }
        self.setValue(mainTabBar, forKey: "tabBar")
        //设置子控制器
        self.setChildViewControllers()
        
    }
    //基于OC动态派发消息
    @objc private func btnClick(){
//        print("点击了按钮")
        let coverView = HMComposeView()
//        let window = UIApplication.sharedApplication().keyWindow
//        window?.addSubview(coverView)
        coverView.show(targetVC: self)
    }
    private func setChildViewControllers(){
        setChildViewController("首页", imageName: "tabbar_home", vc: HMHomeTableViewController())
        setChildViewController("消息", imageName: "tabbar_message_center", vc: HMMessageTableViewController())
        setChildViewController("发现", imageName: "tabbar_discover", vc: HMDiscoveryTableViewController())
        setChildViewController("我的", imageName: "tabbar_profile", vc: HMProfileTableViewController())
    }
    private func setChildViewController(title: String,imageName: String,vc: UIViewController){
        
        vc.tabBarItem = HMTabBarItem()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        let nav = HMNavigationController(rootViewController: vc)
        self.addChildViewController(nav)
        
    }

}

